// pages/user_end/server_info/server_info.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');
const WxParse = require('../../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token:'',
    id:0,
    service:[],
    login:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.id);
    this.setData({
        id:options.id
    })
    this.getService(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token:res.token
          })
          that.getService(that.data.id);
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.service.name,
      desc: '点击查看详情',
      imageUrl:that.data.service.image_text,
      path: '/pages/user_end/server_info/server_info?id='+that.data.id 
      // 路径，传递参数到指定页面。
     }
  },
  goCollect(){
    var that = this;
    if(that.data.login==0){
       wx.navigateTo({
         url: '../../authorization/authorization',
       })
       return false;
    }
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      service_id:that.data.id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.setcollect, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if(res.data.code==1){
        setTimeout(function(){
          that.getService(that.data.id);
        },1500)
       }else if(res.data.code==401){

          wx.navigateTo({
            url: '../../authorization/authorization',
          })
          return false;
       }
    })  	
    
  },
  getService(id){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      id:that.data.id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.servicedetail, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      that.setData({
        service:res.data.data
      })

      if(!res.data.data){
         wx.showToast({
           title:'该服务不存在或已下架!',
           icon:'none'
         });			
         setTimeout(function(){
          wx.navigateBack({
             delta:1
           })
         },1500)
      }

      let content = res.data.data.content;
      WxParse.wxParse('articlecontent', 'html', content, that, 0)
    })  	
  },	
  goComment(){
      wx.navigateTo({
        url:'../server_comment/server_comment?id='+this.data.id
      })
  },
  goOrder(){
	  wx.requestSubscribeMessage({
      tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
    if(!this.data.token){
      wx.navigateTo({
        url:"../../authorization/authorization"
      })
      return false;
    }
    var id = this.data.id;
	wx.navigateTo({
	  url:'../payorder/payorder?id='+id
	})
    // wx.requestSubscribeMessage({
    //     tmplIds: [
    //       'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao'
    //     ],
    //     success:(res)=> {
    //      wx.navigateTo({
    //         url:'../payorder/payorder?id='+id
    //       })										
    //     },
    //     fail:(res)=> {
    //       wx.navigateTo({
    //         url:'../payorder/payorder?id='+id
    //       })								
    //     },
    //   })

  },
  goIndex(){
    wx.reLaunch({
      url: '../index/index',
    })
  }
  
})